package com.capgemini.teamtwo.controller;


import com.capgemini.teamtwo.common.ResponseBean;
import com.capgemini.teamtwo.entity.Dict;
import com.capgemini.teamtwo.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.capgemini.teamtwo.common.ResponseBean;

import javax.servlet.http.HttpSession;

//@RequestBody 用于接受前端传过来的json数据
//@RequestParam() 可以有多个 但是body只能有一个
//如果参数时放在请求体中，传入后台的话，那么后台要用@RequestBody才能接收到
//如果不是放在请求体中的话，那么后台接收前台传过来的参数时，要用@RequestParam来接收
@Api
@RestController
@RequestMapping("/dict")
@Slf4j
public class DictController extends  BaseController {
    @Autowired
    private DictService dictService;

    @ApiOperation("新增字典")
    @PostMapping("/add_dict")
    public ResponseBean<Dict> addDict(@RequestBody Dict dict, HttpSession session) {
        Integer id = getIdFromSession(session);
        String username = getUsernameFromSession(session);
        return ResponseBean.success(dictService.addDict(dict,username));

    }

    @ApiOperation("删除字典")
    @GetMapping("/delete/{dictId}")

    public ResponseBean deldeteDict(@PathVariable("dictId")  Integer dictId){

        return  ResponseBean.success(dictService.deldeteDict(dictId));

    }

    @ApiOperation("编辑字典")
    @PostMapping("/edit_dict")
    // RequestBody只能读取一次，不能多次读取
    public ResponseBean<Dict> updateDictById(@RequestBody Dict dict, HttpSession session) {
        Integer id = getIdFromSession(session);
        String username = getUsernameFromSession(session);
        return ResponseBean.success(dictService.updateDictById(dict.getDictId(),dict,username));

    }

    @ApiOperation("查询字典")
    @PostMapping("/select_dict")
    public ResponseBean<Dict> selectDict(@RequestBody Dict dict) {

        return ResponseBean.success(dictService.selectDict(dict.getDictLable(), dict.getDictValue(),
                dict.getDictType(),dict.getDictName()));

    }

    @ApiOperation("根据name查询")
    @GetMapping("/select_byName ")
    public ResponseBean<Dict> selectByType(@RequestParam  String dictName) {

        return ResponseBean.success(dictService.selectByName(dictName));

    }



}
